local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.ModelObject());
local minPath="ReplicatedStorage.Mine"
local ConnectClass=_G.CommonOHOH.load.Connect();
local mine_cfg=_G.CommonOHOH.config.Mine
local HttpService = game:GetService("HttpService")

local ColliderGroup = CommonOHOH.load.ColliderGroup();
local ColliderManager = CommonOHOH.load.ColliderManager();

local TweenService = game:GetService("TweenService")


-- 定义目标大小
local targetSize = Vector3.new(0.1, 0.1, 0.1)

-- 创建 Tween
local tweenInfo = TweenInfo.new(
    2, -- 持续时间（秒）
    Enum.EasingStyle.Linear, -- 缓动方式
    Enum.EasingDirection.Out -- 缓动方向
)

 function gGetMineObject(id)
    local path= string.format("%s.%s.Model",minPath,id)
    local model= CommonOHOH:GetPath_Loop(path):Clone()
    for _,p in pairs( model:GetDescendants()) do
--原来
        -- if p:IsA("BasePart") then
        --     p.Anchored=true
        --     p.CanTouch=false
        --     p.CanCollide=false
        --     if p.Name=="Instance" then
        --         p.CanTouch=true
        --     end
        -- end
---改后
        if p:IsA("BasePart") then
            p.Anchored=true
            p.CanTouch=false
            p.CanCollide=true
            if p.Name=="Instance" then
                p.CanTouch=true
                p.CanCollide=false
            end
        end
    end
    model.PrimaryPart=model:FindFirstChild("Instance")
    return model
 end

 function CreateGuid()
	return HttpService:GenerateGUID(false)
end


-- 矿石对象
function M.new(...)
	return M(...);
end;

function M.ctor(self,id)--构造函数
    self.DeadEvent=ConnectClass.new() -- 死亡
    self.HurtEvent=ConnectClass.new() -- 受伤
    self.AttackEvent=ConnectClass.new() -- 受伤
    self.Guid=CreateGuid()
    self.cfg=mine_cfg[id]
    local model=gGetMineObject(self.cfg.perfab)

    self.Hp=self.cfg.hp
    self.Damge=self.cfg.damge
	M.super.ctor(self,model);
    self.attribute.IsCarTouch=true
    self:AddPrivateComponentByFolder(script.com)

    ColliderManager:SetAllGroup(self.main_inst, ColliderGroup.monster);
end;

function M.dtor(self)
    self.DeadEvent:dtor()
    self.HurtEvent:dtor()
    self.AttackEvent:dtor()

	M.super.dtor(self);
    if self.Obj then
        self.Obj:Destroy()
        self.Obj=nil
    end
end;


function M.GetDamge(self)
    return  self.Damge
end

function M.GetHp(self)
    return  self.Hp
end

function M.GetItem(self)
    local key = "base_diamond"
    local count = self.cfg.diamond
    return  key,count
end
--受伤
function M.Hurt(self,damge,attackerList)
    self.Hp=self.Hp-damge
    self.HurtEvent:Action(damge,attackerList)
    if self.Hp<=0 then
        self.Hp=0
        self:Dead()
    end
end


function M.GetGuid(self)
    return self.Guid
end

function M.Installation(self,cf,parent)
    self.main_inst:PivotTo(cf)
    self.main_inst.Parent=parent
end

function M.Dead(self)
   
    self.Obj=self.main_inst
    self:AddProcess(function()
        
        
        self:UnLock()
    
        local explosion = Instance.new("Explosion")
        explosion.BlastRadius = 80--爆炸范围
        explosion.BlastPressure =40000 --爆炸力度
        explosion.ExplosionType = Enum.ExplosionType.Craters
        explosion.Position = self.main_inst:GetPivot().Position + Vector3.new(0,-5,-5)
        explosion.Parent = workspace
        self.DeadEvent:Action()
            -- ItemCtrl:AddItem(FavoriteReward)
        task.wait(5)
        self:dtor()
    end)
   
end

--解锁固定的矿石
function M.UnLock(self)
    for _,p in pairs( self.main_inst:GetDescendants()) do

        if p:IsA("BasePart")  then
            p.Anchored=false
            p.CanTouch=false
            p.CanCollide=false
            p.Transparency = 1
            if p.Name~="Instance" then
                
                p.Parent = self.main_inst
                p.Transparency = 0
                p.CanCollide=true
                
            end
            self:AddProcess(function()
                task.wait(1)
                local tween = TweenService:Create(p, tweenInfo, {Size = targetSize})
                -- 播放 Tween
                tween:Play()
            end)
        end
    end

end
-----
--解锁固定的矿石
function M.SetCanCollide(self,CanCollide)
    self.main_inst.Instance.CanCollide=CanCollide
end

return M